/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 *
 * @author patricksamson236
 */
public class FormatOutput {

    private double MINUTE = 60;
    private double HOUR = MINUTE * 60;
    private double DAY = HOUR * 24;
    private double WEEK = DAY * 7;
    private double YEAR = 31556926;
    private int PRECISION = 2;
    private double S_TO_NS = 1000000000; // s/ns ratio
    private long S_TO_MS = 1000; // s/ms ratio
    private double MS_TO_NS = 1000000; // s/ms ratio

    public FormatOutput() {
    }

    public String percentage(double d) {
        String s = d + "";
        if (s.contains(".")) {
            s = s.substring(0, s.indexOf(".") + PRECISION);
        } else {
            //add the dot if it's not there
            s += ".";
        }

        // adjust precision
        while (s.length() - s.indexOf(".") <= PRECISION) {
            s += "0";
        }

        //add the unit
        s += "%";

        return s;
    }

    public String size(double d) {
        String s = "";
        String multi = "";
        if (d > 1024) {
            d = d / 1024;
            if (d > 1024) {
                d = d / 1024;
                if (d > 1024) {
                    d = d / 1024;
                    if (d > 1024) {
                        d = d / 1024;
                        if (d > 1024) {
                            d = d / 1024;
                        } else {
                            multi = " TB";
                        }
                    } else {
                        multi = " GB";
                    }
                } else {
                    multi = " MB";
                }
            } else {
                multi = " KB";
            }
        } else {
            multi = " B";
        }

        //add the size to the string
        s += d;

        //format the string
        if (s.contains(".")) {
            s = s.substring(0, s.indexOf(".") + 2);
        } else {
            //add the dot if it's not there
            s += ".";
        }

        // adjust precision
        while (s.length() - s.indexOf(".") <= PRECISION) {
            s += "0";
        }

        //add the size unit
        s += multi;

        return s;
    }

    public String speed(double d) {
        String s = "";
        String multi = "";
        if (Double.isInfinite(d)) {
            s = "Infinity";
        } else {
            // 0 @ 1 KB/s
            if (d > 1024) {
                d = d / 1024;
                // 1 KB/s @ 10 MB/s
                if (d > 1024) {
                    d = d / 1024;
                    // 10 MB/s @ 1 GB/s
                    if (d > 10240) {
                        d = d / 1024;
                        // 1 GB/s @ 1 TB/s
                        if (d > 1024) {
                            d = d / 1024;
                            // 1 TB/s & ++
                            if (d > 1024) {
                                d = d / 1024;
                            } else {
                                multi = " TB/s";
                            }
                        } else {
                            multi = " GB/s";
                        }
                    } else {
                        multi = " MB/s";
                    }
                } else {
                    multi = " KB/s";
                }
            } else {
                multi = " B/s";
            }

            //add the speed to the string
            s += d;

            //format the string
            if (s.contains(".")) {
                s = s.substring(0, s.indexOf(".") + PRECISION);
            } else {
                //add the dot if it's not there
                s += ".";
            }

            // adjust precision
            while (s.length() - s.indexOf(".") <= PRECISION) {
                s += "0";
            }

            //add the speed unit
            s += multi;
        }
        return s;
    }

    /**
     * @param d time to format in seconds
     * @return the properly formated time
     */
    public String time(double d) {
        String s = "";

        if (Double.isInfinite(d)) {
            s = "Infinity";
        } else {
            d = Math.ceil(d);

            double years = Math.floor(d / YEAR);
            d = d % YEAR;
            if (years > 0) {
                s += years + "y ";
            }

            double weeks = Math.floor(d / WEEK);
            d = d % WEEK;
            if (weeks > 0 || !s.isEmpty()) {
                s += weeks + "w ";
            }

            double days = Math.floor(d / DAY);
            d = d % DAY;
            if (days > 0 || !s.isEmpty()) {
                s += days + "d ";
            }

            double hours = Math.floor(d / HOUR);
            d = d % HOUR;
            if (hours > 0 || !s.isEmpty()) {
                s += hours + "h ";
            }

            double minutes = Math.floor(d / MINUTE);
            d = d % MINUTE;
            if (minutes > 0 || !s.isEmpty()) {
                s += minutes + "m ";
            }

            double seconds = d;
            s += seconds + "s";

            s = s.replace(".0", "");
        }
        return s;
    }

    public String date(long l) {
        String s = "";
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(l);

        s += cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.ENGLISH) + " ";
        s += cal.get(Calendar.DAY_OF_MONTH) + " ";
        s += cal.get(Calendar.YEAR) + ", ";
        s += cal.get(Calendar.HOUR_OF_DAY) + ":";
        s += cal.get(Calendar.MINUTE) + ":";
        s += cal.get(Calendar.SECOND);

        //add necessary zeros
        if (s.substring(s.indexOf(",")).length() < 10) {
            if (s.substring(s.indexOf(","), s.indexOf(":")).length() < 4) {
                s = s.replaceFirst(", ", ", 0");
            }
            if (s.substring(s.indexOf(":"), s.lastIndexOf(":")).length() < 3) {
                s = s.replaceFirst(":", ":0");
            }
            if (s.substring(s.lastIndexOf(":")).length() < 3) {
                s = s.substring(0, s.indexOf(",") + 8)
                        + "0" + s.substring(s.indexOf(",") + 8);
            }
        }
        return s;
    }

    public double nsTOs(long ns) {
        return ns / S_TO_NS;
    }

    public double sTOns(double s) {
        return s * S_TO_NS;
    }

    public long sTOms(double s) {
        return Math.round(s * S_TO_MS);
    }
}
